part of '../repository.dart';

const _kWordTagsCefrLevelKey = 'cefr_level';
const _kWordTagsOpalSKey = 'opal_s';
const _kWordTagsOpalWKey = 'opal_w';

class _WordTagsDto extends Equatable {
  const _WordTagsDto._internal({
    required this.cefrLevelDto,
    required this.opalS,
    required this.opalW,
  });

  final _CefrLevelDto? cefrLevelDto;
  final bool opalS;
  final bool opalW;

  @override
  List<Object?> get props {
    return [
      cefrLevelDto,
      opalS,
      opalW,
    ];
  }

  factory _WordTagsDto.fromJson(Map<String, dynamic> json) {
    final cefrLevelString = json[_kWordTagsCefrLevelKey] as String?;

    _CefrLevelDto? cefrLevelDto;

    if (cefrLevelString != null) {
      cefrLevelDto = _CefrLevelDto.fromString(cefrLevelString);
    }

    return _WordTagsDto._internal(
      cefrLevelDto: cefrLevelDto,
      opalS: json[_kWordTagsOpalSKey],
      opalW: json[_kWordTagsOpalWKey],
    );
  }

  factory _WordTagsDto.fromEntity(WordTags tags) {
    _CefrLevelDto? cefrLevelDto;

    if (tags.cefrLevel != null) {
      cefrLevelDto = _CefrLevelDto.fromEntity(tags.cefrLevel!);
    }

    return _WordTagsDto._internal(
      cefrLevelDto: cefrLevelDto,
      opalS: tags.opalS,
      opalW: tags.opalW,
    );
  }

  Map<String, dynamic> toJson() {
    final json = <String, dynamic>{
      _kWordTagsOpalSKey: opalS,
      _kWordTagsOpalWKey: opalW,
    };

    if (cefrLevelDto != null) {
      json[_kWordTagsCefrLevelKey] = cefrLevelDto.toString();
    }

    return json;
  }

  WordTags toEntity() {
    return WordTags(
      cefrLevel: cefrLevelDto?.toEntity(),
      opalS: opalS,
      opalW: opalW,
    );
  }
}
